import os
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from aws_model.athena_submit import submit

def get_exists_materials(table, date):
    sql = f"select distinct material from {table} where p_date = '{date}'"
    query_rs = submit(sql)
    if query_rs['statusCode'] == 500:
        raise Exception(f"查询sql错误：{sql}")
    materials = []
    if 'result' in query_rs:
        rows = query_rs['result']
        m = []
        for row in rows:
            m.append(row[0])
            if len(m) > 98:
                materials.append(m)
                m = []
        if len(m) > 0:
            materials.append(m)
    return materials
